/*
 * AfterSaleItemView.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.ui.orderabout.aftersale.AfterSaleActivity;
import com.yidiandao.business.ui.widget.dialog.BusinessListDialog;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.UIUtils;

/**
 * 申请售后的商品信息列表
 */
public class AfterSaleItemView extends LinearLayout {
    Context context;
    private CheckBox mCbSelect;
    private ImageView mIvGoodsImage;
    private TextView mTvTitle;
    private TextView mTvPrice;
    private TextView mTvCount;
    private RelativeLayout mRlBecause;
    private TextView mTvSaleExchange;
    private GoodsCountMenu mGoodsCountMenu;
    private EditText mEtDesc;
    private View mView;
    private int mInteger;

    public AfterSaleItemView(Context context) {
        this(context, null);
    }

    public AfterSaleItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        mView = View.inflate(context, R.layout.item_after_sale_view, this);

        mCbSelect = (CheckBox) mView.findViewById(R.id.cb_select);
        mIvGoodsImage = (ImageView) mView.findViewById(R.id.iv_goods_image);
        mTvTitle = (TextView) mView.findViewById(R.id.tv_title);
        mTvPrice = (TextView) mView.findViewById(R.id.tv_price);
        mTvCount = (TextView) mView.findViewById(R.id.tv_count);
        mRlBecause = (RelativeLayout) mView.findViewById(R.id.rl_because);
        mTvSaleExchange = (TextView) mView.findViewById(R.id.tv_sale_exchange);
        mGoodsCountMenu = (GoodsCountMenu) mView.findViewById(R.id.goods_count_menu);
        mEtDesc = (EditText) mView.findViewById(R.id.et_desc);
        mGoodsCountMenu.setNoMoreDataHintText(UIUtils.getString(R.string.to_stock_max));

        initEvent();
    }

    private void initEvent() {
        final AfterSaleActivity activity = (AfterSaleActivity) context;
        mRlBecause.setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final String styles[] = UIUtils.getStringArray(R.array.after_sale_style);
                        new BusinessListDialog.Builder(activity)
                                .setItems(
                                        styles,
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                mTvSaleExchange.setText(styles[which]);
                                                if (which == 0 || which == 2) {
                                                    mGoodsCountMenu.setGoodsCount(mInteger);
                                                    mGoodsCountMenu.setSubtractButtonEnable(false);
                                                } else {
                                                    mGoodsCountMenu.setGoodsCount(1);
                                                    mGoodsCountMenu.setSubtractButtonEnable(false);
                                                }
                                            }
                                        })
                                .create()
                                .show();
                    }
                });
    }

    public void setGoodsLogo(Context context, String url) {
        ImageLoader.loadShopLogo(context, mIvGoodsImage, url);
    }

    public void setName(String name) {
        mTvTitle.setText(name);
    }

    public void setPrice(String price) {
        mTvPrice.setText(price);
    }

    public void setMaxAndMin(String max, int minNumber) {
        mInteger = Integer.parseInt(max);
        mGoodsCountMenu.setMaxGoodsCount(mInteger);
        mGoodsCountMenu.setMinGoodsCount(minNumber);
    }

    public void setDialogData(String title, String tip) {
        mGoodsCountMenu.setChangeGoodsDialogTitle(title);
        mGoodsCountMenu.setDialogOverStockTip(tip);
    }

    public boolean getIsCheck() {
        return mCbSelect.isChecked();
    }

    public int getNumber() {
        return mGoodsCountMenu.getGoodsCount();
    }

    public void setNumber(String number) {
        mTvCount.setText(number);
    }

    public String getDesc() {
        return mEtDesc.getText().toString().trim();
    }

    public String getStyle() {
        return mTvSaleExchange.getText().toString().trim();
    }
}
